package com.ruoyi.chat.controller;

import com.ruoyi.chat.constant.RagFlowUrl;
import com.ruoyi.chat.domain.Dataset;
import com.ruoyi.chat.service.IDatasetService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-24
 */
@RestController
@RequestMapping("/system/dataset")
public class DatasetController extends BaseController
{
    @Autowired
    private IDatasetService datasetService;

    /**
     * 查询知识库信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:dataset:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dataset dataset)
    {
        startPage();
        List<Dataset> list = datasetService.selectDatasetList(dataset);
        return getDataTable(list);
    }

    /**
     * 导出知识库信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:dataset:export')")
//    @Log(title = "知识库信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Dataset dataset)
//    {
//        List<Dataset> list = datasetService.selectDatasetList(dataset);
//        ExcelUtil<Dataset> util = new ExcelUtil<Dataset>(Dataset.class);
//        util.exportExcel(response, list, "知识库信息数据");
//    }

    /**
     * 获取知识库信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dataset:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(datasetService.selectDatasetById(id));
    }

    /**
     * 新增知识库信息
     */
    @PreAuthorize("@ss.hasPermi('system:dataset:add')")
    @Log(title = "知识库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dataset dataset) throws Exception {
        return toAjax(datasetService.add(dataset, RagFlowUrl.DATASETS));
    }

    /**
     * 修改知识库信息
     */
    @PreAuthorize("@ss.hasPermi('system:dataset:edit')")
    @Log(title = "知识库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dataset dataset)
    {
        return toAjax(datasetService.update(dataset, RagFlowUrl.UPDATE_DATASET.replace("{dataset_id}", String.valueOf(dataset.getRagDatasetId()))));
    }

    /**
     * 删除知识库信息
     */
    @PreAuthorize("@ss.hasPermi('system:dataset:remove')")
    @Log(title = "知识库信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(datasetService.delete(ids, RagFlowUrl.DELETE_DATASET));
    }
}
